package com.hspedu.newData.dmsxl.链表.快慢指针;

import com.hspedu.newData.dmsxl.ds.ListNode;
import org.junit.Test;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-04-22 
 * @Version: 1.0
 */

public class LC19删除链表的倒数第N个节点 {

    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummy = new ListNode(-1,head);
        ListNode fast = dummy;
        ListNode slow = dummy;
        for (int i = 0; i < n; i++) {
            fast = fast.next;
        }

        while(fast.next != null){
            fast = fast.next;
            slow = slow.next;
        }
        slow.next = slow.next.next;
        return dummy.next;
    }


    @Test
    public void test() {
        // 测试代码
        ListNode l1 = new ListNode(6, null);
        ListNode l2 = new ListNode(5, l1);
        ListNode l3 = new ListNode(4, l2);
        ListNode l4 = new ListNode(3, l3);
        ListNode l5 = new ListNode(2, l4);
//        ListNode l6 = new ListNode(1, l5);

        removeNthFromEnd(l5,2);
        System.out.println("111");
    }

}
